<? $link=base_url()."finanzas/notasc/dataedit/show/";
		$ano=date("Y");
        $mes=date("m");
		
		$ano2=date("Y");
		$mes2=date("m")-1;
        
        $num=$this->datasis->dameval("SELECT RIGHT((numeroi+1),5)valor FROM gser WHERE YEAR(fecha)='$ano' AND MONTH(fecha)='$mes' AND seriei='010' AND comp='L' ORDER BY numeroi DESC LIMIT 1");
		
	    $num2=$this->datasis->dameval("SELECT RIGHT((numeroi),5)valor FROM gser WHERE YEAR(fecha)='$ano2' AND MONTH(fecha)='$mes2' AND seriei='010' AND comp='L' ORDER BY numeroi DESC LIMIT 1");
		
        $num=str_pad($num,5,'0', STR_PAD_LEFT);       
        if($num=='00000' OR $num==''){
			$num='00001';
		}
		
		$num2=str_pad($num2,5,'0', STR_PAD_LEFT);       
        if($num2=='00000' OR $num2==''){
			$num2='00001';
		}
		
		$mes2=str_pad($mes2,2,'0', STR_PAD_LEFT); 
		$numerod2=$ano2.$mes2.$num2;
		$numerod=$ano.$mes.$num;
		
?>
<script language="javascript" type="text/javascript">
$(function(){
	$(".inputnum").numeric(".");
	//totalizar();
});
function marcar(source,cant){
		//alert(cant);
	    totalg=0;
	    cant=cant+1;
		checkboxes=document.getElementsByTagName('input'); //obtenemos todos los controles del tipo Input
        for(i=0;i<checkboxes.length;i++) //recoremos todos los controles
        {
			
            if(checkboxes[i].type == "checkbox") //solo si es un checkbox entramos
            {
                checkboxes[i].checked=source.checked; //si es un checkbox le damos el valor del checkbox que lo llamó (Marcar/Desmarcar Todos)
                
                if(source.checked){
					
					for (var j = 0; j < cant; j++) {
						deuda = $("#deuda_"+j).val();
						pago  = $("#pago_"+j).val(deuda);
						
					}

				}else{
					
					for (var j = 0; j < cant; j++) {
						
						pago  = $("#pago_"+j).val(00);
						
					}
				}
				
				
            }
        }
        
    for (var i = 1; i < cant; i++) {
	 
     var totals = $("#pago_"+i).val();
     totals = parseFloat(totals);
     totalg += totals;
     //alert(totalg);
     
	}
    var totalg = roundNumber(totalg,2);
    $("#monto").val(totalg);
    $("#monto_val").text(totalg);   
       
} 
function totalizar (id,count,check){
	 
 var totals=0;
 var totalg=0;
 var cant=0;
 
  
 var deuda = $("#deuda_"+id).val();
  
 var totals = $("#pago_"+id).val();
 
 var monto  = $("#monto").val();
 
 
 if(check==true){
	var pago= $("#pago_"+id).val(deuda);
 }else{
    var pago= $("#pago_"+id).val(0);
 }
  
  totals = parseFloat(totals);
  monto  = parseFloat(monto);
  cant   = parseInt(cant)
  cant=count+1;
 
 for (var i = 1; i < cant; i++) {
	 
     var totals = $("#pago_"+i).val();
     totals = parseFloat(totals);
     totalg += totals;
     //alert(totalg);
     
 }

  

  //ssubt = totals+monto; 
 
 $("#monto").val(totalg);
 $("#monto_val").text(totalg);
 
}
function totalizar_2 (id,count){
	
 var totals=0;
 var totalg=0;
 var cant=0;
  
 var deuda = $("#deuda_"+id).val();
  
 var totals = $("#pago_"+id).val();
 
 var monto  = $("#monto").val();
  
  
  totals = parseFloat(totals);
  monto  = parseFloat(monto);
  cant   = parseInt(cant)
  cant=count+1;
 
  //alert (cant);
  
   
 if(totals>deuda)
  alert('Pago mayor a la DEUDA PENDIENTE, Por favor verifique los montos ');
 
 for (var i = 1; i < cant; i++) {
	 
     var totals = $("#pago_"+i).val();
     totals = parseFloat(totals);
     totalg += totals;
     //alert(totalg);
     
 }
 
 $("#monto").val(totalg);
 $("#monto_val").text(totalg);
}
</script>
<form id="form1" name="form1" method="post" action="<?php echo site_url("finanzas/notasc/apliguarda/"); ?>">
<table width='70%' align="center" >
    <tr>
		  <td colspan="8" align='right'>
		<input name="aplica" type="button" id="aplica" value="Ir Atras" class="button" onclick="self.location='<?=$link ?><?= $id?>'";  />
		<input name="aplica" type="submit" id="aplica" value="Guardar" class="button" />
		</td>
    </tr>
<tr><td colspan='9'>
	<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
		<legend class="titulofieldset" style='color: #2E4B77;'>Informaci&ograve;n del Movimiento</legend>	
	<table width='100%'>
	<tr>
	<td class="littletablerow" align='right' width='30%'>Comprobante L010-DIARIOS</td>
	<td class="littletablerow" >
		<input name="numeroc" type="text" id="numeroc" value="<?=$numerod?>" size="12" maxlength="11" class="input"/></td>
	<td class="littletablerow" ><span style='color:red'><?=$msj?></span></td>
	</tr>
	<tr>
		<td class="littletablerow" align='right'>Fecha de Comprobante</td>
		<td class="littletablerow">
		<input name="fechac" type="text" id="fechac" value="<?= date('Y-m-d') ?>" size="12" maxlength="11" class="input"/></td>
	    <td class="littletablerow"><span style='color:green'>Ultimo comprobante del mes <?= "(".$ano2."/".$mes2.")" ?><?= " ".$numerod2 ?></span></td>
	</tr>
</table>
</fieldset>
</td></tr>
	<tr>
		<td colspan='9'>Monto sin aplicar: <? echo nformat($mresta)?> </td>
	</tr>
	<tr class="littletablerow" align="center">
		<td colspan='9' align='left'><input type="checkbox" onclick="marcar(this,<?=$cantmd?>);" /> Marcar/Desmarcar todas las Facturas Pendientes</td>
    </tr>
	<tr class="littletablerow" align="center">
		<td colspan="2" bgcolor='#527D9E' >N&uacute;mero de Factura</td>
		<td bgcolor='#527D9E'>Fecha</td>
		<td bgcolor='#527D9E'>Moneda</td>
		<td bgcolor='#527D9E'>Total</td>
		<td bgcolor='#527D9E'>Abonos</td>
		<td bgcolor='#527D9E'>Monto Disponible</td>
		<td bgcolor='#527D9E'>Monto Aplicar</td>
	</tr>
	<? $tmonto=0; $tabonos=0; $tdeudas=0; $i=0; $count=count($result); foreach ($result AS $items){$i++;?>
    <? $tmonto+=$items->monto ?>
    <? $tabonos+=$items->abono ?>
    <? $tdeudas+=$items->monto-$items->abono ?> 
    <?$pago='0';?>
     <input type="hidden" name="cant"       value="<?=$count ?>"/>
     <input type="hidden" name="id"         value="<?=$id ?>"/>   
     <input type="hidden" name="serief[]"   value="<?=$items->seriep?>"/>
	 <input type="hidden" name="factura[]"  value="<?=$items->numero?>"/>
	 <input type="hidden" name="proveedor[]" value="<?=$items->proveed?>"/>
	 <input type="hidden" name="cod_prv[]"   value="<?=$items->cod_prv ?>"/>
	 <input type="hidden" name="numerof[]"   value="<?=$items->factura ?>"/>
	 <input type="hidden" name="compro[]"  value="<?=$items->compro ?>"/>
	 <input type="hidden" name="transac[]"  value="<?=$items->transac ?>"/>
	<tr>
		<td class="littletablerow" align="center">
		<input type="checkbox"   id="chek_<?= $i ?>"  onclick="totalizar(<?= $i ?>,<?= $count ?>,this.checked)"  /></td>
	    <td align='center' class="littletablerow"><?= $items->compro ?><?= $items->seriep ?>-<?= $items->numero ?></td>
        <td class="littletablerow" align="center"><?=dbdate_to_human($items->fecha) ?></td>
        <td align='center' class="littletablerow"><?= $items->moneda ?></td>
        <td align='right' class="littletablerow"><?= nformat($items->monto) ?></td>
        <td align='right' class="littletablerow"><?= nformat($items->abono) ?></td>
        <td class="littletablerow" align='right'><input id="deuda_<?= $i ?>"  name="deuda_<?= $i ?>" type="text" size="10" value="<?= $items->monto-$items->abono ?>" readonly="false" align="right"  /></td>
        <td class="littletablerow" align='right'><input id="pago_<?= $i ?>"   name="pago[]"  type="text" size="10" value="<?= $pago ?>" onkeyup="totalizar_2(<?= $i ?>,<?= $count ?>)" align="right" /></td>      
	</tr>
	<?}?>
	<tr>
      <td colspan="4" align='right' class="littletablerow" ><strong>Totales</strong></td>
      <td align='right' class="littletablerow"><strong><? echo nformat($tmonto) ?></strong></td>
      <td align='right' class="littletablerow"><strong><? echo nformat($tabonos) ?></strong></td>
      <td class="littletablerow" align='right'><strong><? echo nformat($tdeudas) ?></strong></td>
    </tr>
    <tr>
	  <td colspan="8" >
	  <fieldset style='border: 2px outset #9AC8DA;background: #EFEFEF;'>
	  <legend  class="titulofieldset" style='color: #2E4B77;' align="right">-- Monto Total Aplicar --</legend>
	  <table width='100%'  align='center' >
	  	<tr>
	  		<td align='right' class="littletablerow"><strong><b id='monto_val'><input id="monto"   name="monto"  type="text" size="10" readonly="false" value="0.00" /></b></strong></td>
	  </table>
	  </fieldset>	
	  </td>
    </tr>
</table>
</form>
<table width='50%' align="center" >
	<tr class="littletablerow" align="center">
		<td colspan='7' align='left'>Total de Facturas Aplicadas</td>
    </tr>
	<tr class="littletablerow" align="center">
		<td bgcolor='#527D9E' >N&uacute;mero de Factura</td>
		<td bgcolor='#527D9E'>Monto Aplicado</td>
		<td bgcolor='#527D9E'>Aplicado el d&iacute;a</td>
		<td bgcolor='#527D9E'>N&uacute;mero</td>
		<td bgcolor='#527D9E'>Acci&oacute;n</td>
	</tr>
	<? $tmonto=0; $tabonos=0; foreach ($aplicadas AS $items2){$i++;?>
    <? $tmonto+=$items2->monto ?>
    <input type="hidden" name="cobro" value="<?=$items2->cobro ?>"/>
	<tr>
	    <td align='center' class="littletablerow"><?= $items2->serie ?>-<?= $items2->numero ?></td>
        <td align='right' class="littletablerow"><?= nformat($items2->monto) ?></td>
        <td class="littletablerow" align="center"><?=dbdate_to_human($items2->fecha) ?></td>
		<td align="center" class="littletablerow">L010-<?= $items2->numerod ?></td>
        <td class="littletablerow" align="center">
<a href="<?php echo base_url() ?>/finanzas/notasc/delete/<?=$id?>/<?= $items2->transac ?>" ><?php echo img("images/delete.jpg"); ?></a></td>
	</tr>
	<?}?>
	<tr>
      <td align='right' class="littletablerow" ><strong>Totales</strong></td>
      <td align='right' class="littletablerow"><strong><? echo nformat($tmonto) ?></strong></td>
    </tr>
</table>
